document.addEventListener('DOMContentLoaded', function() {
    // 获取验证码按钮功能
    const getCodeBtn = document.getElementById('getCodeBtn');
    if (getCodeBtn) {
        getCodeBtn.addEventListener('click', function() {
            const emailInput = document.getElementById('email');
            const email = emailInput.value.trim();
            
            if (!email) {
                alert('请输入邮箱地址');
                return;
            }
            
            if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
                alert('请输入有效的邮箱地址');
                return;
            }
            
            // 模拟发送验证码
            let countdown = 60;
            getCodeBtn.disabled = true;
            getCodeBtn.textContent = `${countdown}秒后重新获取`;
            
            const timer = setInterval(() => {
                countdown--;
                getCodeBtn.textContent = `${countdown}秒后重新获取`;
                
                if (countdown <= 0) {
                    clearInterval(timer);
                    getCodeBtn.disabled = false;
                    getCodeBtn.textContent = '获取验证码';
                }
            }, 1000);
            
            alert(`验证码已发送至 ${email}（模拟）`);
        });
    }
    
    // 注册表单验证
    const registerForm = document.getElementById('registerForm');
    if (registerForm) {
        registerForm.addEventListener('submit', function(e) {
            e.preventDefault();
            
            const email = document.getElementById('email').value.trim();
            const code = document.getElementById('verificationCode').value.trim();
            const username = document.getElementById('username').value.trim();
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirmPassword').value;
            
            // 简单验证
            if (!email || !code || !username || !password || !confirmPassword) {
                alert('请填写所有字段');
                return;
            }
            
            if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
                alert('请输入有效的邮箱地址');
                return;
            }
            
            if (password.length < 6) {
                alert('密码长度不能少于6位');
                return;
            }
            
            if (password !== confirmPassword) {
                alert('两次输入的密码不一致');
                return;
            }
            
            // 模拟注册成功
            alert('注册成功！即将跳转到首页（模拟）');
            // window.location.href = 'index.html';
        });
    }
    
    // 导航栏激活状态
    const currentPage = window.location.pathname.split('/').pop();
    const navLinks = document.querySelectorAll('.nav-links a');
    
    navLinks.forEach(link => {
        const linkPage = link.getAttribute('href').split('/').pop();
        if (linkPage === currentPage) {
            link.classList.add('active');
        } else {
            link.classList.remove('active');
        }
    });
});